﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using FTools.Dominio;
using System.Data;
using System.Collections;
using FTools.Generico;

namespace FTools.Persistencia
{
    class PerNumerador
    {
        static string msg = "";

        //OBTENER NUMERADOR
        public Numerador obtenerNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, String xTabla)
        {
            Numerador objNumerador = new Numerador();
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select * from NUMERADOR where TABLA=@Tabla");

                command.Parameters.AddWithValue("@Tabla", xTabla);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];

                    objNumerador.Tabla = (String)fila.ItemArray[0]; objNumerador.Tabla = objNumerador.Tabla.Trim();
                    objNumerador.Numero = (long)fila.ItemArray[1];
                }
                return objNumerador;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE NUMERADOR
        public Boolean existeNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, String xTabla)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from NUMERADOR where TABLA=@Tabla");

                command.Parameters.AddWithValue("@Tabla", xTabla);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE NUMERADOR
        public Boolean existeNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, String xTabla, long xNumero)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from NUMERADOR where TABLA=@Tabla and NUMERO=@Numero");

                command.Parameters.AddWithValue("@Tabla", xTabla);
                command.Parameters.AddWithValue("@Numero", xNumero);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA NUMERADOR
        public int altaNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, Numerador xObjNumerador)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into NUMERADOR values (@Tabla, @Numero)");

                command.Parameters.AddWithValue("@Tabla", xObjNumerador.Tabla.Trim());
                command.Parameters.AddWithValue("@Numero", xObjNumerador.Numero);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA NUMERADOR
        public int bajaNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, Numerador xObjNumerador)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from NUMERADOR where TABLA=@Tabla");

                command.Parameters.AddWithValue("@Tabla", xObjNumerador.Tabla.Trim());
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA Numerador
        public int modificaNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, Numerador xObjNumerador)
        {
            try
            {
                SqlCommand command = new SqlCommand("update NUMERADOR set NUMERO=NUMERO + 1 " +
                                                    "where TABLA=@Tabla");

                command.Parameters.AddWithValue("@Tabla", xObjNumerador.Tabla.Trim());

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //GENERAR NUMERADOR
        public long generarNumerador(SqlConnection xConexion, SqlTransaction xTransaccion, String xTabla)
        {
            Numerador objNumerador;
            if (this.existeNumerador(xConexion, xTransaccion, xTabla))
            {
                objNumerador = this.obtenerNumerador(xConexion, xTransaccion, xTabla);
                this.modificaNumerador(xConexion, xTransaccion, objNumerador);
                objNumerador.Numero++;
            }
            else
            {
                objNumerador = new Numerador();
                objNumerador.Tabla = xTabla;
                objNumerador.Numero = 1;
                this.altaNumerador(xConexion, xTransaccion, objNumerador);
            }
            return objNumerador.Numero;
        }
    }
}
